package com.xyoto.community.service.impl;

import com.xyoto.community.constant.*;
import com.xyoto.community.dto.SaveQuestionCommentReplyLoveDTO;
import com.xyoto.community.entity.comment.QuestionComment;
import com.xyoto.community.entity.comment.QuestionCommentReply;
import com.xyoto.community.entity.love.QuestionCommentReplyLove;
import com.xyoto.community.entity.main.Question;
import com.xyoto.community.enums.LoveEnum;
import com.xyoto.community.enums.QuestionsEnum;
import com.xyoto.community.enums.WechatLoginEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.comment.QuestionCommentMapper;
import com.xyoto.community.mapper.comment.QuestionCommentReplyMapper;
import com.xyoto.community.mapper.love.QuestionCommentReplyLoveMapper;
import com.xyoto.community.mapper.main.MessageMapper;
import com.xyoto.community.mapper.main.QuestionMapper;
import com.xyoto.community.service.MessageService;
import com.xyoto.community.service.QuestionCommentReplyLoveService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:   问答评论回复点赞service接口
 * Author:      挽风
 * Date:        2020
 * Copyright:   2020 by 挽风1.0版本
 * Description:
 **/

//@Service
@Slf4j
public class QuestionCommentReplyLoveServiceImpl implements QuestionCommentReplyLoveService {

    @Autowired
    private QuestionCommentReplyLoveMapper questionCommentReplyLoveMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private QuestionCommentReplyLoveMapper replyLoveMapper;

    @Resource
    private QuestionCommentReplyMapper questionCommentReplyMapper;

    @Resource
    private QuestionCommentMapper questionCommentMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Autowired
    private MessageService messageService;

    @Resource
    private MessageMapper messageMapper;

    /**
     * 根据问答评论回复id查询问答评论回复点赞数
     *
     * @param id 问答评论回复id
     *
     * @return 点赞数
     */
    @Override
    public Integer getQuestionCommentReplyLoveCount(Integer id) {
        return questionCommentReplyLoveMapper.getQuestionCommentReplyLoveCount(id);
    }

    /**
     * 新增评论回复点赞
     * @param replyLoveDTO
     * @return
     */
    @Override
    public Result saveQuestionCommentReplyLove(SaveQuestionCommentReplyLoveDTO replyLoveDTO) {

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        if (userId == UserLoginStatus.UN_LOGIN) {ExceptionUtils.error(WechatLoginEnum.USER_NOT_LOGIN);}

        Integer questionCommentReplyId = replyLoveDTO.getQuestionCommentReplyId();
        //判断问答评论回复是否存在
        QuestionCommentReply commentReplyById = questionCommentReplyMapper.getQuestionCommentReplyById(questionCommentReplyId);
        if (commentReplyById == null || commentReplyById.getDeleteStatus() == DeleteStatus.YES){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_NOT_EXISTED);
        }

        //判断问答评论是否存在
        QuestionComment questionCommentById = questionCommentMapper.getQuestionCommentById(commentReplyById.getQuestionCommentId());
        if (questionCommentById == null || questionCommentById.getDeleteStatus() == DeleteStatus.YES){
            ExceptionUtils.error(QuestionsEnum.QUESTION_COMMENTS_NOT_EXIST);
        }

        //判断问答是否存在
        Question questionById = questionMapper.getQuestionById(questionCommentById.getQuestionId());
        if (questionById == null || questionById.getDeleteStatus() == DeleteStatus.YES){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }

        //判断是否已经点过赞
        QuestionCommentReplyLove getReplyLoveStatus = questionCommentReplyLoveMapper.getReplyLoveStatus(questionCommentReplyId,userId);
        if (getReplyLoveStatus != null && getReplyLoveStatus.getStatus() == LoveStatus.LOVE){
            ExceptionUtils.error(LoveEnum.QUESTION_COMMENT_REPLY_LOVED);
        }

        //若上述条件都不满足则执行新增问答评论回复点赞功能
        log.info("开始执行新增评论回复点赞功能");
        QuestionCommentReplyLove replyLove = new QuestionCommentReplyLove();
        replyLove.setQuestionCommentReplyId(questionCommentReplyId);
        replyLove.setStatus(QuestionCommentReplyLoveStatus.LOVE);
        replyLove.setUserId(userId);

        log.info("开始执行SQL");
        Integer saveReplyLove = replyLoveMapper.saveReplyLove(replyLove);
        if (saveReplyLove == null || saveReplyLove ==0) {
            ExceptionUtils.error(LoveEnum.QUESTION_COMMENT_REPLY_FAIL);
        }

//        //开始存入消息
//        messageService.saveMessage(userId,questionById.getUserId(),replyLove.getId(),
//                ModuleType.QUESTION_COMMENT_REPLY_LOVE,questionById.getId());

        return Result.success();
    }

    /**
     * 取消问答评论回复点赞
     * @param questionCommentReplyId 问答评论回复id
     * @return
     */
    @Override
    @Transactional
    public Result removeQuestionCommentReplyLove(Integer questionCommentReplyId){
        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        //判空检验
        if (null == questionCommentReplyId || questionCommentReplyId <= 0){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_ID_ERROR);
        }
        QuestionCommentReply questionCommentReply = questionCommentReplyMapper
                .getQuestionCommentReplyById(questionCommentReplyId);
        //校验问答评论回复是否存在
        checkQuestionCommentReply(questionCommentReply);
        QuestionComment questionComment = questionCommentMapper
                .getQuestionCommentById(questionCommentReply.getQuestionCommentId());
        //检验问答评论是否存在
        checkQuestionComment(questionComment);
        Question question = questionMapper.getQuestionById(questionComment.getQuestionId());
        //检验问答是否存在
        checkQuestion(question);
        //检验点赞状态
        QuestionCommentReplyLove replyLove = questionCommentReplyLoveMapper
                .getReplyLove(questionCommentReplyId, userId);
        if(null == replyLove){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_LOVE_IS_NOT_EXIST);
        }
        if (replyLove.getStatus().equals(DeleteStatus.NO)){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_LOVE_IS_EXIST);
        }
        //取消点赞业务逻辑
        QuestionCommentReplyLove questionCommentReplyLove = new QuestionCommentReplyLove();
        questionCommentReplyLove.setQuestionCommentReplyId(questionCommentReplyId);
        questionCommentReplyLove.setStatus(QuestionCommentReplyLoveStatus.DELETE_LOVE);
        questionCommentReplyLove.setUserId(userId);
        log.info("执行SQL");
        Integer state = questionCommentReplyLoveMapper.
                removeQuestionCommentReplyLove(questionCommentReplyLove);
        log.info("SQL执行完成");
        if (state != OperationStatus.SUCCESS){
            ExceptionUtils.error(LoveEnum.QUESTION_COMMENT_REPLY_FAIL);
        }
//        //删除点赞消息
//        Message message = new Message();
//        message.setType(ModuleType.QUESTION_COMMENT_REPLY_LOVE);
//        message.setModuleId(replyLove.getId());
//        message.setThirdParentId(question.getId());
//        Integer status = messageMapper
//                .removeMessage(message);
//        if (status != OperationStatus.SUCCESS){
//            ExceptionUtils.error(MessageEnum.DELETED_QUESTION_COMMENT_REPLY_LOVE_MESSAGE_FAILED);
//        }
        return Result.success();
    }

    /**
     * 校验问答评论回复是否存在
     * @param questionCommentReply
     */
    private void checkQuestionCommentReply(QuestionCommentReply questionCommentReply){
        if (null == questionCommentReply){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_NOT_EXISTED);
        }
        //检验问答评论回复是否已删除
        if (questionCommentReply.getDeleteStatus().equals(DeleteStatus.YES)){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_IS_DELETE);
        }
    }

    /**
     * 校验问答评论是否存在
     * @param questionComment
     */
    private void checkQuestionComment(QuestionComment questionComment){
        if (null == questionComment){
            ExceptionUtils.error(QuestionsEnum.QUESTION_COMMENTS_NOT_EXIST);
        }
        //检验问答评论是否删除
        if (questionComment.getDeleteStatus().equals(DeleteStatus.YES)){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_IS_DELETE);
        }
    }

    /**
     * 校验问答是否存在
     * @param question
     */
    private void checkQuestion(Question question) {
        if (null == question) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }
        //检验问答是否删除
        if (question.getDeleteStatus().equals(DeleteStatus.YES)) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_DELETE);
        }
    }
}
